package by.blog.dao.dao;

import by.blog.dao.beans.Film;
import by.blog.dao.dao.factory.MysqlDaoFactory;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Бондарчик Антон  15.11.2014.
 */
public class DaoFilm implements DaoInterface<Film, Integer> {

    /**
     * This fields are used for creating SQL query to table.
     * There are name of table and column names of this table.
     */
    private String tableName = "films";
    private String columnName1 = "film_id";
    private String columnName2 = "film_name";
    private String columnName3 = "category_id";
    private String columnName4 = "user_name";

    /**
     * This method insert (Film) film into table of DB.
     * @param film
     * @return true if the operation was successful, if else  - return false.
     */
    public boolean createUnit(Film film) {

        try {
            String create = "INSERT INTO " + tableName + " ( " +
                    columnName1 + ", " +
                    columnName2 + ", " +
                    columnName3 + ", " +
                    columnName4 +
                    " ) VALUES (?, ?, ?, ?)";

            PreparedStatement statement = MysqlDaoFactory.prepareStatement(create);

            statement.setInt(1, film.getFilm_id());
            statement.setString(2, film.getFilm_name());
            statement.setInt(3, film.getCategory_id());
            statement.setString(4, film.getUser_name());

            statement.executeUpdate();
            return true;
        }
        catch (SQLException e) {

            return false;
        }
    }

    /**
     * This method returns film from DB by his index.
     * Method use load() to create object (Film) film
     * @param INDEX
     * @return (Film) film
     */
    public Film readUnit(Integer INDEX) {

        try {
            String read = "SELECT * FROM " + tableName + " WHERE " + columnName1 + "=?";
            PreparedStatement statement = MysqlDaoFactory.prepareStatement(read);

            statement.setInt(1, INDEX);

            ResultSet result = statement.executeQuery();

            Film film = null;

            while ((result).next()) {
                film = load(result);
            }
            return film;
        }
        catch (SQLException e) {

            return null;
        }
    }

    /**
     * This method create object (Film) film via ResultSet.
     * @param RS (ResultSet)
     * @return Film, created from ResultSet, or null if the operation wasn't successful.
     * @throws SQLException
     */
    private static Film load(ResultSet RS)  throws SQLException {
        Film film = new Film();
        try {
            film.setFilm_id(RS.getInt(1));
            film.setFilm_name(RS.getString(2));
            film.setCategory_id(RS.getInt(3));
            film.setUser_name(RS.getString(4));
        }
        catch (SQLException e) {
            return null;
        }
        return film;
    }

    /**
     * This method update row in DB
     * @param film
     * @return true if the operation was successful, if else  - return false.
     */

    public boolean updateUnit(Film film) {
        try {
            String update = "UPDATE " + tableName + " SET "
                    + columnName2 + " =?, "              //name
                    + columnName3 + " =?, "              //category_id
                    + columnName4  + " =? WHERE "        //user_id
                    + columnName1 + "=?";

            PreparedStatement statement = MysqlDaoFactory.prepareStatement(update);

            statement.setString(1, film.getFilm_name());
            statement.setInt(2, film.getCategory_id());
            statement.setString(3, film.getUser_name());
            statement.setInt(4, film.getFilm_id());
            statement.executeUpdate();

            return true;
        } catch (SQLException e) {

            return false;
        }
    }

    /**
     * This method delete film from DB by index.
     * @param INDEX
     * @return true if the operation was successful, if else  - return false.
     */
    public boolean deleteUnit(Integer INDEX) {
        try {
            String del = "DELETE FROM " + tableName + " WHERE " + columnName1 + "=?";
            PreparedStatement statement = MysqlDaoFactory.prepareStatement(del);

            statement.setInt(1, INDEX);
            statement.executeUpdate();

            return true;
        }
        catch (SQLException e) {

            return false;
        }
    }

    /**
     * This method select objects by PARENT_ID and returns them in an ArrayList
     * @param PARENT_ID
     * @return ArrayList with objects, which have a PARENT_ID that we need
     */
    public List<Film> readUnitsByParent(int PARENT_ID) {
        try {
            String read = "SELECT * FROM " + tableName + " WHERE " + columnName3 + " =?";

            PreparedStatement statement = MysqlDaoFactory.prepareStatement(read);
            statement.setInt(1, PARENT_ID);

            ResultSet result = statement.executeQuery();

            return loadUnits(result);
        }
        catch (SQLException e) {

            return null;
        }
    }

    /**
     * This method select all objects from table and returns them in an ArrayList
      * @return ArrayList with all objects from table
     */
    public List<Film> readAllUnits() {
        try {
            String read = "SELECT * FROM " + tableName;

            PreparedStatement statement = MysqlDaoFactory.prepareStatement(read);
            ResultSet result = statement.executeQuery();

            return loadUnits(result);
        }
        catch (SQLException e) {

            return null;
        }
    }

    /**
     * This method used to make readUnitsByParent() and readAllUnits() easier.
     * @param RS
     * @return ArrayList with objects from ResultSet
     */
    private static List<Film> loadUnits(ResultSet RS) {
        try {
            List<Film> filmArrayList = new ArrayList<Film>();
            Film film;

            while ((RS).next()) {

                film = load(RS);
                filmArrayList.add(film);
            }
            return filmArrayList;
        }
        catch (SQLException e) {
            return null;
        }
    }

}
